package com.jeeplus.modules.project.web;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.project.entity.BaseProjectInfo;
import com.jeeplus.modules.project.entity.CourseGuide;
import com.jeeplus.modules.project.entity.vo.ProjectStatisticsVo;
import com.jeeplus.modules.project.service.BaseProjectInfoService;
import com.jeeplus.modules.project.service.CourseGuideService;
import com.jeeplus.modules.project.service.ProjectApplyService;
import com.jeeplus.modules.project.service.ProjectKnotService;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.service.OfficeService;

/**
 * 主页
 * @author jacky
 * @version 2017-12-19
 */
@Controller
@RequestMapping(value = "${adminPath}/project/home")
public class ProjectIndexController extends BaseController {
	@Autowired
	private BaseProjectInfoService baseProjectInfoService;
	@Autowired
	private ProjectApplyService projectApplyService;
	@Autowired
	private ProjectKnotService projectKnotService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private CourseGuideService courseGuideService;
	
	@RequestMapping(value = "index")
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
		// 机构列表
		List<Office> officeList = officeService.findNotRootOffice();
		model.addAttribute("officeList", officeList);
		BaseProjectInfo info = new BaseProjectInfo();
		//info.setStatus(1);
		Page<BaseProjectInfo> page = new Page<BaseProjectInfo>();
		page.setOrderBy("a.create_date");
		page.setPageSize(8);
		page = baseProjectInfoService.findPage(page, info);
		List<BaseProjectInfo> projectList = page.getList();
		model.addAttribute("projectList", projectList);
		//课题指南
		Page<CourseGuide> page1 = new Page<CourseGuide>();
		page1.setOrderBy("a.create_date");
		page1.setPageSize(8);
		page1 = courseGuideService.findPage(page1, new CourseGuide());
		List<CourseGuide> guideList = page1.getList();
		model.addAttribute("guideList", guideList);
		
		return "modules/project/projectIndex";
	}
	/**
	 * 历年申请立项结项统计柱状图数据
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "echartbar")
	@ResponseBody
	public Map<String,Object> echartbar(HttpServletRequest request, HttpServletResponse response, Model model) {
		String deptId = request.getParameter("deptId");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("deptId", deptId);
		List<ProjectStatisticsVo> list = projectApplyService.projectApplyStatistics(map);
		List<ProjectStatisticsVo> list2 = baseProjectInfoService.projectSetUpStatistics(map);
		List<ProjectStatisticsVo> list3 = projectKnotService.projectKnotStatistics(map);
		//封装x坐标
		Calendar cal = new GregorianCalendar();
		Integer year = cal.get(Calendar.YEAR);
		Integer [] xAxis = new Integer[] {year-3,year-2,year-1,year};
		//申请
		Map<String, Object> series = new HashMap<String, Object>();
		List<Integer> apply = new LinkedList<Integer>();
		for(Integer y : xAxis){
			boolean flag = false;
			for(ProjectStatisticsVo vo : list){
				if(y.intValue() == vo.getYear().intValue()){
					apply.add(vo.getCount());
					flag = true;
				}
			}
			if(!flag){
				apply.add(0);
			}
		}
		series.put("申请", apply);
		List<Integer> setup = new LinkedList<Integer>();
		for(Integer y : xAxis){
			boolean flag = false;
			for(ProjectStatisticsVo vo : list2){
				if(y.intValue() == vo.getYear().intValue()){
					setup.add(vo.getCount());
					flag = true;
				}
			}
			if(!flag){
				setup.add(0);
			}
		}
		series.put("立项", setup);
		List<Integer> knot = new LinkedList<Integer>();
		for(Integer y : xAxis){
			boolean flag = false;
			for(ProjectStatisticsVo vo : list3){
				if(y.intValue() == vo.getYear().intValue()){
					knot.add(vo.getCount());
					flag = true;
				}
			}
			if(!flag){
				knot.add(0);
			}
		}
		series.put("结项", knot);
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}
	
	@RequestMapping(value = "projectLevelBar")
	@ResponseBody
	public Map<String,Object> projectLevelBar(HttpServletRequest request, HttpServletResponse response, Model model) {
		String deptId = request.getParameter("deptId");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("deptId", deptId);
		map.put("projectLevel", 1);
		List<ProjectStatisticsVo> list1 = baseProjectInfoService.projectLevelStatistics(map);
		map.put("projectLevel", 2);
		List<ProjectStatisticsVo> list2 = baseProjectInfoService.projectLevelStatistics(map);
		map.put("projectLevel", 3);
		List<ProjectStatisticsVo> list3 = baseProjectInfoService.projectLevelStatistics(map);
		map.put("projectLevel", 4);
		List<ProjectStatisticsVo> list4 = baseProjectInfoService.projectLevelStatistics(map);
		//封装x坐标
		Calendar cal = new GregorianCalendar();
		Integer year = cal.get(Calendar.YEAR);
		Integer [] xAxis = new Integer[] {year-3,year-2,year-1,year};
		Map<String, Object> series = new HashMap<String, Object>();
		List<Integer> country = new LinkedList<Integer>();
		for(Integer y : xAxis){
			boolean flag = false;
			for(ProjectStatisticsVo vo : list1){
				if(y.intValue() == vo.getYear().intValue()){
					country.add(vo.getCount());
					flag = true;
				}
			}
			if(!flag){
				country.add(0);
			}
		}
		series.put("国家级", country);
		
		List<Integer> zizhiqu = new LinkedList<Integer>();
		for(Integer y : xAxis){
			boolean flag = false;
			for(ProjectStatisticsVo vo : list2){
				if(y.intValue() == vo.getYear().intValue()){
					zizhiqu.add(vo.getCount());
					flag = true;
				}
			}
			if(!flag){
				zizhiqu.add(0);
			}
		}
		series.put("自治区", zizhiqu);
		
		List<Integer> yuanji = new LinkedList<Integer>();
		for(Integer y : xAxis){
			boolean flag = false;
			for(ProjectStatisticsVo vo : list3){
				if(y.intValue() == vo.getYear().intValue()){
					yuanji.add(vo.getCount());
					flag = true;
				}
			}
			if(!flag){
				yuanji.add(0);
			}
		}
		series.put("院级", yuanji);
		
		List<Integer> other = new LinkedList<Integer>();
		for(Integer y : xAxis){
			boolean flag = false;
			for(ProjectStatisticsVo vo : list4){
				if(y.intValue() == vo.getYear().intValue()){
					other.add(vo.getCount());
					flag = true;
				}
			}
			if(!flag){
				other.add(0);
			}
		}
		series.put("其他", other);
		
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}
	
	/**
	 * 首页立项结项统计折线图数据
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "eZhe")
	@ResponseBody
	public Map<String,Object> echartZhe(HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status", 5);
		List<ProjectStatisticsVo> list = baseProjectInfoService.projectStatusStatistics(map);
		map.put("status", 8);
		List<ProjectStatisticsVo> list2 = baseProjectInfoService.projectStatusStatistics(map);
		//封装x坐标
		Calendar cal = new GregorianCalendar();
		Integer year = cal.get(Calendar.YEAR);
		Integer [] xAxis = new Integer[] {year-3,year-2,year-1,year};
		
		Map<String, Object> series = new HashMap<String, Object>();
		
		//立项
		List<Integer> setup = new LinkedList<Integer>();
		for(Integer y : xAxis){
			boolean flag = false;
			for(ProjectStatisticsVo vo : list){
				if(y.intValue() == vo.getYear().intValue()){
					setup.add(vo.getCount());
					flag = true;
				}
			}
			if(!flag){
				setup.add(0);
			}
		}
		series.put("立项", setup);
		
		List<Integer> knot = new LinkedList<Integer>();
		for(Integer y : xAxis){
			boolean flag = false;
			for(ProjectStatisticsVo vo : list2){
				if(y.intValue() == vo.getYear().intValue()){
					knot.add(vo.getCount());
					flag = true;
				}
			}
			if(!flag){
				knot.add(0);
			}
		}
		series.put("结项", knot);
		
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}
	
	
}